package euler.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class IncreasingPrimesHelper {

    private BufferedReader br;
    private int lastPrime;
    private String[] split;
    private int currentFile;

    public IncreasingPrimesHelper() {
    	init();
    }

    private void init() {
        this.lastPrime = 0;
        this.split = new String[0];
        this.br = null;
        this.currentFile = 0;
    }

	public boolean isPrime(int n) {
	    try {
			return isIncreasingPrime(n);
		} catch (IOException e) {
			throw new IllegalArgumentException("isPrime",e);
		}
    }

	private boolean isIncreasingPrime(int n) throws IOException {
        if (n <= 1) {
            throw new IllegalArgumentException("Parametro menor que 2");

        } else if (n < this.lastPrime) {
            this.close();
            this.init();
        } else if (this.currentFile != 1 && n > 1 && n <= 179424673) {
            if (this.br != null) {
                this.br.close();
            }
            this.currentFile = 1;
            this.br = new BufferedReader(new FileReader(new File(Utils.pathToFiles + "primes" + 1 + ".txt")));

        } else if (this.currentFile != 2 && n > 179424673 && n <= 373587883) {
            if (this.br != null) {
                this.br.close();
            }
            this.currentFile = 2;
            this.br = new BufferedReader(new FileReader(new File(Utils.pathToFiles + "primes" + 2 + ".txt")));

        } else if (this.currentFile != 3 && n > 373587883 && n <= 573259391) {
            if (this.br != null) {
                this.br.close();
            }
            this.currentFile = 3;
            this.br = new BufferedReader(new FileReader(new File(Utils.pathToFiles + "primes" + 3 + ".txt")));

        } else if (this.currentFile != 4 && n > 573259391 && n <= 776531401) {
            if (this.br != null) {
                this.br.close();
            }
            this.currentFile = 4;
            this.br = new BufferedReader(new FileReader(new File(Utils.pathToFiles + "primes" + 4 + ".txt")));

        } else if (this.currentFile != 5 && n > 776531401 && n <= 982451653) {
            if (this.br != null) {
                this.br.close();
            }
            this.currentFile = 5;
            this.br = new BufferedReader(new FileReader(new File(Utils.pathToFiles + "primes" + 5 + ".txt")));

        } else if (n > 982451653) {
            throw new IllegalArgumentException("Parametro mayor " + n + " que el maximo 982451653");
        }

        for (int i = 0; i < this.split.length; i++) {
            String trimmed = this.split[i].trim();
            if (trimmed.length() > 0) {
                int m = Integer.parseInt(this.split[i]);
                if (m == n) {
                    return true;
                } else if (m > n){
                    return false;
                }
            }
        }

        this.lastPrime = n;

        String strLine;
        while ((strLine = this.br.readLine()) != null)   {
            this.split = strLine.split(" ");

            for (int i = 0; i < this.split.length; i++) {
                String trimmed = this.split[i].trim();
                if (trimmed.length() > 0) {
                    int m = Integer.parseInt(this.split[i]);
                    if (m == n) {
                        return true;
                    } else if (m > n){
                        return false;
                    }
                }
            }
        }

        throw new IllegalArgumentException("Parametro mayor que el maximo");
    }


    public void close() throws IOException {
        if (this.br != null) {
            this.br.close();
        }
    }
}